package com.rain.cy.business.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.google.common.base.Preconditions;
import com.rain.cy.business.model.dto.UserWorkDTO;
import com.rain.cy.business.model.dto.UserWorkSearchRoomDTO;
import com.rain.cy.business.model.entity.UserWorkEntity;
import com.rain.cy.business.service.UserWorkService;
import com.rain.cy.framework.entity.BaseController;
import com.rain.cy.framework.result.Result;
import com.rain.cy.framework.result.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yuchen
 * @date 2021年10月4日00:08:56
 */
@RestController
@RequestMapping("api/work")
public class WorkController extends BaseController {

    @Autowired
    private UserWorkService userWorkService;

    /**
     * 标记任务
     */
    @PostMapping("mark")
    public Result markWork(@RequestBody UserWorkDTO userWorkDTO) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(userWorkDTO.getWorks()), "任务不能为空");
        UserWorkEntity workEntity = userWorkService.lambdaQuery().eq(UserWorkEntity::getUserId, userWorkDTO.getUserId())
                .eq(UserWorkEntity::getWorkDate, userWorkDTO.getWorkDate()).last("limit 1").one();
        if (workEntity!=null){
            workEntity.setWorks(userWorkDTO.getWorks());
        }else {
            workEntity = BeanUtil.copyProperties(userWorkDTO, UserWorkEntity.class);
        }
        Integer doctorUserId = getCurrentUserId();
        workEntity.setDoctorUserId(doctorUserId);
        if (StringUtils.containsAny(userWorkDTO.getWorks(), "3003", "3004")) {
            workEntity.setSearchRoom(true);
            workEntity.setSearchRoomStatus(1);
        }
        return ResultUtil.successOrFail(userWorkService.saveOrUpdate(workEntity));
    }

    @PostMapping("remark")
    public Result remark(@RequestBody UserWorkSearchRoomDTO userWorkSearchRoomDTO) {
        Preconditions.checkArgument(userWorkSearchRoomDTO.getId() != null, "用户ID不能为空");
        Preconditions.checkArgument(StringUtils.isNotEmpty(userWorkSearchRoomDTO.getRemark()), "备注不能为空");
        UserWorkEntity userWorkEntity = userWorkService.getById(userWorkSearchRoomDTO.getId());
        userWorkEntity.setSearchRoomRemark(userWorkSearchRoomDTO.getRemark());
        userWorkEntity.setSearchRoomStatus(userWorkSearchRoomDTO.getStatus());
        return ResultUtil.successOrFail(userWorkService.saveOrUpdate(userWorkEntity));
    }


    /**
     * 查询任务
     *
     * @param userId 患者ID
     * @return 任务列表
     */
    @GetMapping("query")
    public Result queryWork(Integer userId) {
        Preconditions.checkArgument(userId != null, "用户ID不能为空");
        List<UserWorkEntity> userWorkEntities = userWorkService.lambdaQuery().eq(UserWorkEntity::getUserId, userId).list();
        return ResultUtil.success(userWorkEntities);
    }

    /**
     * 查询任务
     *
     * @param doctorId 医生ID
     * @return 任务列表
     */
    @GetMapping("doctor/query")
    public Result queryDoctorWork(Integer doctorId) {
        Preconditions.checkArgument(doctorId != null, "医生ID不能为空");
        List<UserWorkEntity> userWorkEntities = userWorkService.lambdaQuery().eq(UserWorkEntity::getDoctorUserId, doctorId).list();
        return ResultUtil.success(userWorkEntities);
    }

}
